home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_u_z / wshell11.zip / COPY.C < prev    next >
Text File  |  1991-11-20  |  10KB  |  353 lines

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <direct.h>
  5. #include <dos.h>
  6. #include <time.h>
  7. #include <ctype.h>
  8. #include "wstdio.h"
  9. #include "wslib.h"
  10. #include "copy.h"
  11.  
  12. #define CF_BUFFSIZE     1000000
  13. #define CF_MAX_READ     65535
  14.  
  15. HANDLE hMod;
  16.  
  17. /***************************************************************************/
  18. /* FUNCTION: LibMain - Entry function for DLL as defined by libentry.asm   */
  19. /* USES    : parameters ignored                                            */
  20. /* DESCRIPTION: this function is called by Windows to initailize the DLL   */
  21. /*-------------------------------------------------------------------------*/
  22. int FAR PASCAL LibMain (hModule, wDataSeg, cbHeapSize, lpszCmdLine)
  23.    HANDLE   hModule ;
  24.    WORD     wDataSeg ;
  25.    WORD     cbHeapSize ;
  26.    LPSTR    lpszCmdLine ;
  27. {
  28.    if (cbHeapSize != 0)
  29.        UnlockData (0);
  30.    hMod = hModule;
  31.    return 1;
  32. }
  33.  
  34. /***************************************************************************/
  35. /* FUNCTION: WEP                                                           */
  36. /* USES    : parameters ignored                                            */
  37. /* NOTES   : Function called by windows to destroy DLL                     */
  38. /*-------------------------------------------------------------------------*/
  39. void FAR PASCAL WEP (bSystemExit)
  40. int      bSystemExit ;
  41. {
  42.      return ;
  43. }
  44.  
  45. #define LINELEN 130
  46.  
  47. int FAR PASCAL ModuleProc (HWND hwndDisplay, int argc, LPSTR argv[])
  48. {
  49.    BOOL bVerbose   = TRUE;    // command line flags
  50.  
  51.    HANDLE hCopyBuff;          // buffer for copy
  52.    char   huge *lpCopyBuff;
  53.  
  54.    struct find_t fi;          // holds file info 
  55.  
  56.    char szDestFName[LINELEN]; // destination filename string 
  57.    char szSrcFName[LINELEN];  // destination filename string 
  58.    char cMsg[LINELEN];        // used to print out errors 
  59.  
  60.    LPSTR 
  61.         src,               // points to source file name 
  62.         dest,              // points to dest file name 
  63.         destcatptr,        // points end of dest path, where the source 
  64.                            // filename is copied 
  65.                            // if == 0, no dest filename subs done 
  66.         srccatptr;         // points end of src path, where the source 
  67.                            // filename is copied, also serves as flag 
  68.                            // if == 0, no source filename subs done 
  69.    LPSTR parm2;            // pointers to parameter string
  70.  
  71.    dest = szDestFName;     // points to dest file name 
  72.    src  = szSrcFName;      // points to source file name
  73.    destcatptr = 0;
  74.    srccatptr  = 0; 
  75.  
  76.    if (argc < 2)
  77.    {
  78.       dputs (hwndDisplay, "usage: copy <sourcefile> <destfile>\n");
  79.       return TRUE;
  80.    }
  81.  
  82.    lstrcpy (src, argv[1]);  // copy string to local data seg (DLL prob)
  83.    parm2 = argv[2];
  84.  
  85.    if (argc < 3)
  86.    {
  87.       // if only one parameter, use current directory for dest 
  88.       destcatptr = szDestFName; 
  89.    }
  90.    /* two parameters - (current limit for copy) */
  91.    /* is paramter 2 a directory? if so, dest name substituions */
  92.    else if (IsDirectory (parm2))
  93.    {
  94.       LPSTR lpstrBkSlsh;
  95.       lstrcpy (dest, parm2);
  96.  
  97. /*      if (* (dest + lstrlen (dest) - 1) == '.')
  98.          destcatptr = --dest;
  99.       else*/
  100.       {
  101.          lpstrBkSlsh = dest + lstrlen (dest) - 1;
  102.          if (*lpstrBkSlsh != '\\' && *lpstrBkSlsh != ':')
  103.             *(++lpstrBkSlsh) = '\\';
  104.          destcatptr = lpstrBkSlsh + 1;
  105.       }
  106.    }
  107.  
  108.    // is it a drive letter only??
  109.    else if (isalpha (*parm2) && *(parm2+1) == ':' && *(parm2+2) == NULL)
  110.    {
  111.       destcatptr = parm2 + 2;
  112.    }
  113.  
  114.    /* destination is a single file name */
  115.    else
  116.    {
  117.       lstrcpy (dest, parm2);
  118.    }
  119.  
  120.    /* is parameter 1 a directory, if so, append "\*.*" if needed and perform
  121.       source name subs */
  122.    if (IsDirectory (src))
  123.    {
  124. //      if (* (src + lstrlen (src) - 1) == '.')
  125. //         src--;
  126.       if (*(src + lstrlen (src) - 1) != '\\')
  127.          lstrcat (src, "\\*.*");
  128.       else
  129.          lstrcat (src, "*.*");
  130.       srccatptr = src + lstrlen (src) - 3; /* to point before appended "*.*" */
  131.    }
  132.  
  133.    // is it a drive letter only??
  134.    else if (isalpha (*src) && *(src+1) == ':' && *(src+2) == NULL)
  135.    {
  136.       lstrcpy (src + 2, "*.*");
  137.       srccatptr = src + 2;
  138.    }
  139.  
  140.    /* is paramter 1 a wildcard (* or ?) - do name source substitution */
  141.    else if ((srccatptr = lstrchr (src, '\*')) || (srccatptr = lstrchr (src, '\?')))
  142.    {
  143.       /* srccatptr points to * or ?, but need to get it to point to end */
  144.       /* of the path if one specified, or begging of string. Done by waliking */
  145.       /* back through string */
  146.       while (srccatptr > src)    /* what a pain ! */
  147.          if (*srccatptr-- == '\\')
  148.          {
  149.             srccatptr += 2;      /* srccatptr points to char before '\\' */
  150.             break;               /* make it point to one AFTER */
  151.          }
  152.    }
  153.    /* now srccatptr is NULL or points to proper place in string */
  154.  
  155.    /* ok, the shit work aside, the copy loop is nice */
  156.    if (_dos_findfirst (src, _A_NORMAL, &fi) == 0)
  157.    {
  158.       // allocate and lock memory for copy
  159.       if ((hCopyBuff = GlobalAlloc (GMEM_MOVEABLE, CF_BUFFSIZE)) != NULL)
  160.       {
  161.          if ((lpCopyBuff = (char huge *) GlobalLock (hCopyBuff)) != NULL)
  162.          {
  163.             do
  164.             {
  165.                if (destcatptr) 
  166.                   lstrcpy (destcatptr, fi.name);
  167.                if (srccatptr != 0)      
  168.                   lstrcpy (srccatptr, fi.name);
  169.                if (bVerbose)
  170.                {
  171.                   wsprintf ((LPSTR) cMsg, "%s \t=> %s\n", (LPSTR) src, (LPSTR) dest);
  172.                   dputs (hwndDisplay, cMsg);
  173.                }
  174.                switch (CopyFile (hwndDisplay, lpCopyBuff, dest, src))
  175.                {
  176.                   case -1:
  177.                   case 0:
  178.                   break;
  179.       
  180.                   case 1:
  181.                      wsprintf ((LPSTR) cMsg, "Couldn't open source file \"%s\"\n", (LPSTR) src);
  182.                      dputs (hwndDisplay, cMsg);
  183.                   break;
  184.       
  185.                   default:
  186.                      wsprintf ((LPSTR) cMsg, "Couldn't copy \"%s\" to \"%s\"\n", (LPSTR) src, (LPSTR) dest);
  187.                      dputs (hwndDisplay, cMsg);
  188.                   break;
  189.                }
  190.                YieldToOthers ();
  191.             } while (_dos_findnext (&fi) == 0);
  192.  
  193.             GlobalUnlock (hCopyBuff);
  194.          } // if (GlobalLock)
  195.          else
  196.          {
  197.             dputs (hwndDisplay, "Error allocating memory for copy");
  198.          }
  199.          GlobalFree (hCopyBuff);
  200.  
  201.       } // if (GlobalAlloc)
  202.       else
  203.       {
  204.          dputs (hwndDisplay, "Error allocating memory for copy");
  205.       }
  206.    } // if (findfirst)
  207.    else
  208.    {
  209.       wsprintf ((LPSTR) cMsg, "File not found: \"%s\"\n", (LPSTR) src);
  210.       dputs (hwndDisplay, cMsg);
  211.    }
  212.    return TRUE;
  213. }
  214.  
  215. int CopyFile (HWND hwndDisplay, char huge * lpCopyBuff, LPSTR Dest, LPSTR Src)
  216. {
  217.    char cMsg[LINELEN];  /* used to print out errors */
  218.  
  219.    int hDest, hSrc,
  220.        count;
  221.  
  222.    int iChar;
  223.  
  224.    if ( (hSrc = _lopen (Src, OF_READ | OF_SHARE_DENY_WRITE)) == -1)
  225.       return 1;
  226.  
  227. /*
  228.    if (bOverWrite == FALSE)
  229.    {
  230.       if ((hDest = _lopen (Dest, OF_READ)) != -1) 
  231.       {
  232.          _lclose (hDest);
  233.          wsprintf (cMsg, "overwrite existing \"%s\"?\n", (LPSTR) Dest);
  234.          dputs (hwndDisplay, cMsg);
  235.          if (dgets (hwndDisplay, cMsg, sizeof (cMsg)) == -1)
  236.          {
  237.             _lclose (hSrc);
  238.             return -1;
  239.          }
  240.          else if (cMsg[0] != 'y')
  241.          {
  242.             _lclose (hSrc);
  243.             return 2;
  244.          }
  245.       }
  246.    }
  247. */
  248.  
  249.    if ((hDest = _lcreat (Dest, 0)) == -1) 
  250.    {
  251.       _lclose (hSrc);
  252.       return 3;
  253.    }
  254.  
  255.    while (TRUE)
  256.    {
  257.       DWORD SpaceUsed;
  258.       WORD  BytesRead, BytesToWrite;
  259.       char huge *bOut;
  260.  
  261.       //----------------------- read into buffer
  262.       SpaceUsed = 0;
  263.       while ( CF_BUFFSIZE - SpaceUsed > CF_MAX_READ )
  264.       {
  265.          YieldToOthers ();
  266.          BytesRead = _lread (hSrc, lpCopyBuff + SpaceUsed, CF_MAX_READ);
  267.          if (BytesRead == 0)
  268.             break;
  269.          SpaceUsed += BytesRead;
  270.       }
  271.  
  272.       //---------------------- write to output file
  273.       bOut = lpCopyBuff;
  274.       BytesToWrite = CF_MAX_READ;
  275.  
  276.       while ( SpaceUsed > 0 )
  277.       {
  278.          YieldToOthers ();
  279.          if (SpaceUsed < CF_MAX_READ)
  280.             BytesToWrite = (WORD) SpaceUsed;
  281.  
  282.          _lwrite (hDest, bOut, BytesToWrite);
  283.  
  284.          bOut      += (DWORD) BytesToWrite;
  285.          SpaceUsed -= (DWORD) BytesToWrite;
  286.       }
  287.  
  288.       if (BytesRead == 0)
  289.          break;
  290.    }
  291.  
  292.    _lclose (hDest);
  293.    _lclose (hSrc);
  294.    return 0;
  295. }
  296.  
  297. int FAR PASCAL ShowOptions (HWND hwndParent)
  298. {
  299.    FARPROC lpfn;
  300.    lpfn = MakeProcInstance (OptionsDlg, hMod);
  301.    DialogBox (hMod, "OPTIONS", hwndParent, lpfn);
  302.    FreeProcInstance (lpfn);
  303.    return 0;
  304. }
  305.  
  306. int FAR PASCAL ShowAbout (HWND hwndParent)
  307. {
  308.    FARPROC lpfn;
  309.    lpfn = MakeProcInstance (AboutDlg, hMod);
  310.    DialogBox (hMod, "ABOUT", hwndParent, lpfn);
  311.    FreeProcInstance (lpfn);
  312.    return 0;
  313. }
  314.  
  315. int FAR PASCAL OptionsDlg (HWND hDlg, WORD wMsg, WORD wParam, DWORD lParam)
  316. {
  317.    switch (wMsg)
  318.    {
  319.        case WM_CLOSE:
  320.           EndDialog(hDlg, NULL);
  321.           return(TRUE);
  322.       break;
  323.  
  324.       case WM_COMMAND:
  325.          if (wParam == IDOK)
  326.              EndDialog(hDlg, NULL);
  327.           return(TRUE);
  328.       break;
  329.    }
  330.    return FALSE;
  331. }
  332.  
  333. int FAR PASCAL AboutDlg (HWND hDlg, WORD wMsg, WORD wParam, DWORD lParam)
  334. {
  335.    switch (wMsg)
  336.    {
  337.        case WM_CLOSE:
  338.           EndDialog(hDlg, NULL);
  339.           return(TRUE);
  340.       break;
  341.  
  342.       case WM_COMMAND:
  343.          if (wParam == IDOK)
  344.              EndDialog(hDlg, NULL);
  345.           return(TRUE);
  346.       break;
  347.    }
  348.    return FALSE;
  349. }
  350.  
  351.  
  352.  
  353.